package com.alib.springcloud.controller;

import com.alib.springcloud.entities.CommonResult;
import com.alib.springcloud.entities.Payment;
import com.alib.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果：" + payment);

        if( payment != null){
            return new CommonResult(200,"查询成功！服务端口："+serverPort,payment);
        }else{
            return new CommonResult(400,"查询失败！查询ID：" + id,null);
        }
    }

    @PostMapping("/payment/create")
    public CommonResult<Payment> insertPayment(@RequestBody Payment payment){
        int insertResult = paymentService.create(payment);
        log.info("*****插入结果：" + insertResult);

        if( insertResult ==1 ){
            return new CommonResult(200,"插入成功！服务端口："+serverPort, insertResult);
        }else{
            return new CommonResult(400,"插入失败！插入数据：" + payment.toString(),null);
        }
    }


    @GetMapping(value = "/payment/lb")
    public String getLbPayment(){
        return serverPort;
    }

    @GetMapping(value = "/payment/timeout")
    public String getPaymentTimeout(){
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return serverPort;
    }


    // 服务链路监控 测试 ziplin + sleuth
    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "提供者--->" + serverPort + "zipkin ---->";
    }

}
